﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;

namespace Tencent.OA.App
{
    [Serializable]
    [AttributeUsage(AttributeTargets.Enum, Inherited = false, AllowMultiple = true)]
    [ComVisible(false)]
    public class EnumTextAttribute : Attribute
    {
        List<EnumTextItem> _values = new List<EnumTextItem>();

        public EnumTextAttribute(int itemValue, string itemName)
        {
            this._values.Add(new EnumTextItem(itemValue, itemName));
        }

        public EnumTextAttribute(params string[] value)
        {
            for (int i = 0; i < value.Length; i++)
                this._values.Add(new EnumTextItem(i, value[i]));
        }

        public List<EnumTextItem> Values
        {
            get { return this._values; }
        }
    }

    public static class EnumTextExtensions
    {
        public static List<EnumTextItem> GetEnumTextList<TEnum>()
        {
            List<EnumTextItem> list = new List<EnumTextItem>();
            foreach (Attribute attr in Attribute.GetCustomAttributes(typeof(TEnum), typeof(EnumTextAttribute)))
            {
                if (attr.GetType() == typeof(EnumTextAttribute))
                {
                    list.AddRange(((EnumTextAttribute)attr).Values);
                }
            }
            // 按枚举值排序。
            list.Sort();
            return list;
        }

        /// <summary>
        /// <para> 从指定泛型参数(必须为一个枚举类型)类型的属性(Attribute)中，获取应用到该对象的属性值的集合中查找与<paramref name="item"/>对应的项的值。</para>
        /// </summary>
        /// <typeparam name="TEnum"><para> 必须为一个枚举类型，同时该枚举已应用了<see cref="EnumTextAttribute"/>属性。</para></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string GetEnumItemText<TEnum>(this TEnum e)
        {
            if (!typeof(TEnum).IsEnum)
                throw new ArgumentException("泛型参数的类型必须是一个枚举类型。");

            List<EnumTextItem> values = GetEnumTextList<TEnum>();
            string text = null;
            foreach (EnumTextItem enumItem in values)
            {
                int v;
                if (Int32.TryParse(Enum.Format(typeof(TEnum), e, "d"), out v) && enumItem.ItemValue == v)
                    text = enumItem.ItemName;
            }
            return text;
        }

    }

    /// <summary>
    /// <para> 表示用于描述枚举项目对应的字符或值的数据项。</para>
    /// </summary>
    public class EnumTextItem : IComparable<EnumTextItem>
    {
        /// <summary>
        /// <para> 使用指定的值初始化一个EnumTextItem类的新实例。</para>
        /// </summary>
        /// <param name="itemValue"></param>
        /// <param name="itemText"></param>
        public EnumTextItem(int itemValue, string itemText)
        {
            this.ItemValue = itemValue;
            this.ItemName = itemText;
        }

        /// <summary>
        /// <para> 获取或设置数据项的值。</para>
        /// </summary>
        public int ItemValue { get; set; }

        /// <summary>
        /// <para> 获取或设置数据项的名称。</para>
        /// </summary>
        public string ItemName { get; set; }

        public int CompareTo(EnumTextItem other)
        {
            if (other == null)
                return 1;
            return this.ItemValue.CompareTo(other.ItemValue);
        }

    }
}
